/**
 * class  BaseButton
 * 按钮基类
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.support
{
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import as3.component.Component;

	public class BaseButton extends Component
	{
		
		protected var _simpleButton:SimpleButton;
		
		protected var _downState:DisplayObject;
		protected var _overState:DisplayObject;
		protected var _upState:DisplayObject;
		protected var _enableState:DisplayObject;
		protected var _hitTestState:DisplayObject;
		
		protected var _enabled:Boolean = true;
		
		public function BaseButton()
		{
			super();
		}
		
		override protected function initCom():void
		{
			this.cacheAsBitmap = true;
			
			_simpleButton = new SimpleButton();
			_simpleButton.focusRect = false;
			_simpleButton.tabEnabled = false;
			_simpleButton.useHandCursor = false;
			addChild(_simpleButton);
			this.addEventListener(Event.ADDED_TO_STAGE,addTOStageHandler);
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
			updateEnable();
		}
		
		override protected function initListener():void
		{
			super.initListener();
			addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function set downImage(dis:DisplayObject):void
		{
			if (dis && _downState != dis) 
			{
				_downState = dis;
			}
		}
		
		public function set overImage(dis:DisplayObject):void
		{
			if (dis && _overState != dis) 
			{
				_overState = dis;
			}
		}
		
		public function set upImage(dis:DisplayObject):void
		{
			if (dis && _upState != dis) 
			{
				_upState = dis;
			}
		}
		
		public function set disabledImage(dis:DisplayObject):void
		{
			if (dis && _enableState != dis) 
			{
				_enableState = dis;
			}
		}
		
		public function get enableState():DisplayObject
		{
			return _enableState;
		}
		
		public function set hitTestState(dis:DisplayObject):void
		{
			if (dis && _hitTestState != dis) 
			{
				_hitTestState = dis;
				_simpleButton.hitTestState = _hitTestState;
			}
		}
		
		public function set enabled(value:Boolean):void
		{
			if (value != _enabled)
			{
				_enabled = value;
				updateEnable();
			}
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		
		override public function set width(value:Number):void
		{
//			if (simpleButton.upState) simpleButton.upState.width = value;
//			if (simpleButton.overState) simpleButton.overState.width = value;
//			if (simpleButton.downState) simpleButton.downState.width = value;
//			if (enableState) enableState.width = value;
			
			if (_upState) _upState.width = value;
			if (_overState) _overState.width = value;
			if (_downState) _downState.width = value;
			if (_enableState) _enableState.width = value;
			super.width = value;
		}
		
		override public function set height(value:Number):void
		{
//			if (simpleButton.upState) simpleButton.upState.height = value;
//			if (simpleButton.overState) simpleButton.overState.height = value;
//			if (simpleButton.downState) simpleButton.downState.height = value;
//			if (enableState) enableState.height = value;
			
			if (_upState) _upState.height = value;
			if (_overState) _overState.height = value;
			if (_downState) _downState.height = value;
			if (_enableState) _enableState.height = value;
			super.height = value;
		}
		
		protected function updateEnable():void
		{
			_simpleButton.mouseEnabled = _enabled;
			if (_enabled)
			{
				if (_upState) _simpleButton.upState = _upState;
				if (_overState) _simpleButton.overState = _overState;
				if (_downState) _simpleButton.downState = _downState;
				if (_hitTestState) _simpleButton.hitTestState = _overState;
				else 
				{
					_simpleButton.hitTestState = _upState;
				}
//				this.mouseChildren = true;
//				this.mouseEnabled = true;				
			} 
			else 
			{
				_simpleButton.upState = _enableState;
				_simpleButton.overState = _enableState;
				_simpleButton.downState = _enableState;
				_simpleButton.hitTestState = _enableState;
//				this.mouseEnabled = false;
//				this.mouseChildren = false;
			}
		}
		
		protected function clickHandler(me:MouseEvent):void
		{
			if(!_enabled)
				me.stopImmediatePropagation();
		}
		
		private function addTOStageHandler(e:Event):void
		{
			if(_upState != null)
			{
				simpleButton.hitTestState = _upState;
			}
		}
		
		public function get simpleButton():SimpleButton
		{
			return _simpleButton;
		}
		
		override public function updateDisplay():void
		{
			super.updateDisplay();
			
			updateEnable();
		}
	}
}